﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dycox.Validation;

namespace Dycox.Workflows.Conditions
{
    public class CompareCondition : PropertyValueCondition
    {
        public CompareCondition(Dictionary<string, string> parameters)
            : base(parameters)
        {
            string value;

            if (!parameters.TryGetValue("valueToCompare", out value))
                throw new InvalidOperationException("The value which to be compared not set.");

            _valueToCompare = value;

            if (parameters.TryGetValue("operator", out value))
                _operator = (CompareOperator)Enum.Parse(typeof(CompareOperator), value);
        }

        private string _valueToCompare;

        private CompareOperator _operator;

        protected override Validator CreateInnerValidator()
        {
            CompareValidator validator = new CompareValidator();
            validator.Operator = _operator;
            return validator;
        }

        protected override void InitializeInnerValidator(Dycox.Validation.Validator validator, object valueToValidate)
        {
            // TODO: 比较条件对空值的处理
            CompareValidator cv = (CompareValidator)validator;
            cv.ValueToCompare = Dycox.Utils.ConvertUtil.ConvertType(_valueToCompare, valueToValidate.GetType());

        }
    }
}
